<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="信息描述">
        <el-tag type="info">excel模版说明</el-tag>
        <el-tag>
          <i class="el-icon-download"/>
          <a :href="OSS_PATH + 'excelTemplate/%E5%AF%BC%E5%85%A5%E8%AF%BE%E7%A8%8B%E6%A8%A1%E6%9D%BF.xls'">点击下载excel模版</a>
        </el-tag>
      </el-form-item>
      <el-form-item label="选择Excel">
        <el-upload
          ref="upload"
          :auto-upload="false"
          :on-success="fileUploadSuccess"
          :on-error="fileUploadError"
          :on-remove="handleRemove"
          :on-change="handleChange"
          :on-exceed="handleUploadExceed"
          :disabled="importBtnDisabled"
          :limit="1"
          :action="BASE_API+'/eduservice/edu-subject/addSubject'"
          :file-list="fileList"
          name="file"
          accept="application/vnd.ms-excel">
          <el-button slot="trigger" size="small" type="primary">选取文件</el-button>

          <el-button
            :loading="loading"
            style="margin-left: 10px;"
            size="small"
            type="success"
            @click="submitUpload">上传到服务器</el-button>
        </el-upload>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  //export表示可以被别人调用
  export default {
    data(){
      return {
        BASE_API: process.env.BASE_API, // 接口API地址
        OSS_PATH: process.env.OSS_PATH, // 阿里云OSS地址
        importBtnDisabled: false, // 选取文件按钮是否禁用,
        loading: false,
        fileList:[], // 上传的文件列表
        flagCount:0 //文件数量标识
      }
    },
    created(){

    },
    methods: {
      //点击按钮上传文件到接口里面
      submitUpload() {
        if (this.flagCount <=0) {
          this.$message({
            type: 'warning',
            message: '请选择要导入的课程列表的excel文件'
          })
          return
        }
        this.importBtnDisabled = true
        this.loading = true
        // 表单提交，相当于js: document.getElementById("upload").submit()
        this.$refs.upload.submit()
      },
      handleChange(file, fileList) {
        //file.response == undefined 说明不是成功时回调的改变事件
        if (file.response == undefined && fileList.length > 0) {
          this.flagCount = this.flagCount+1
        }
      },
      handleRemove(){
        this.flagCount = this.flagCount-1
      },
      handleUploadExceed() {
        this.$message.warning('请先删除下方已选取的文件')
      },
      //上传成功
      fileUploadSuccess(response) {
        this.flagCount = this.flagCount-1
        this.loading = false
        this.importBtnDisabled = false
        if (response.success == false){
          this.fileList = []
          this.$message({
            type: 'error',
            message: '添加课程分类失败，请严格按照模板样式'
          });
        }else{
          this.$message({
            type: 'success',
            message: '添加课程分类成功'
          })
          //跳转课程分类列表
          //路由跳转
          this.$router.push({path:'/subject/list'})
        }
      },
      //上传失败
      fileUploadError() {
        this.flagCount = this.flagCount-1
        this.fileList = []
        this.importBtnDisabled = false
        this.loading = false
        this.$message({
          type: 'error',
          message: '添加课程分类失败，请严格按照模板样式'
        })
      }

    }
  }
</script>

<style scoped>

</style>
